//
//  YZSwineHeaderView.m
//  VET
//
//  Created by 刘文靖 on 2018/8/2.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZSwineHeaderView.h"
@interface YZSwineHeaderView()
@property (nonatomic, assign) YZSwineHeaderViewType type;
@end
@implementation YZSwineHeaderView

- (instancetype)initWithType:(YZSwineHeaderViewType)type{
    self = [super init];
    if (self) {
        _type = type;
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    UIButton *leftButton = [[UIButton alloc] init];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"ico_back_24white"] forState:UIControlStateNormal];
    [leftButton setTitleColor:kColorWithHex(0xffffff) forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    [self addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10 + kStatusBarHeight);
        make.left.offset(11);
        make.size.mas_equalTo(CGSizeMake(60, 25.5));
    }];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textColor = kColorWithHex(0xffffff);
    titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    [self addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(leftButton);
        make.centerX.offset(0);
        make.height.mas_equalTo(25);
    }];
    
    UIButton *rightButton = [[UIButton alloc] init];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"ico_add copy"] forState:UIControlStateNormal];
    [self addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(leftButton);
        make.right.offset(-17);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    
    UIButton *rightSearchBtn = [[UIButton alloc] init];
    [rightSearchBtn setBackgroundImage:[UIImage imageNamed:@"ico_search"] forState:UIControlStateNormal];
    [self addSubview:rightSearchBtn];
    [rightSearchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.with.height.mas_equalTo(rightButton);
        make.right.mas_equalTo(rightButton.mas_left).offset(-12);
    }];
    rightSearchBtn.alpha = 0;
    self.rightSearchBtn = rightSearchBtn;
    
    UIButton *searchBtn = [[UIButton alloc] init];
    searchBtn.backgroundColor = [UIColor whiteColor];
    searchBtn.layer.cornerRadius = 4;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ico_Searchbox_search"]];
    [searchBtn addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(16, 16));
        make.left.offset(12);
        make.centerY.offset(0);
    }];
    UILabel *searchLabel = [[UILabel alloc] init];
    searchLabel.textColor = kColorWithHex(0xD9D9D9);
    searchLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    [searchBtn addSubview:searchLabel];
    [searchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imageView.mas_right).offset(6);
        make.centerY.offset(0);
    }];
    [self addSubview:searchBtn];
    [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(32);
        make.left.offset(27);
        make.right.offset(-27);
        make.height.mas_equalTo(48);
    }];
    self.searchBtn = searchBtn;
    
    if (_type == YZSwineHeaderViewSwine) {
        self.layer.contents = (__bridge id)[UIImage imageNamed:@"pig_background"].CGImage;
        titleLabel.text = @"猪只健康卡";
        searchLabel.text = @"输入猪只耳标号";
    }else if (_type == YZSwineHeaderViewHerd){
        self.layer.contents = (__bridge id)[UIImage imageNamed:@"herd_background"].CGImage;
        titleLabel.text = @"猪群健康卡";
        searchLabel.text = @"输入猪群批次号";
    }else{
        self.layer.contents = (__bridge id)[UIImage imageNamed:@"dieout_background"].CGImage;
        titleLabel.text = @"死淘管理卡";
        searchLabel.text = @"输入耳标号或者批次号";
    }
    @weakify(self)
     [[leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
         @strongify(self)
         if (self.delegate && [self.delegate respondsToSelector:@selector(backButtonClicked)]) {
             [self.delegate backButtonClicked];
         }
     }];
    [[rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(menuButtonClick:)]) {
            [self.delegate menuButtonClick:rightButton];
        }
    }];
    [[rightSearchBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(searchButtonClicked)]) {
            [self.delegate searchButtonClicked];
        }
    }];
    [[searchBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(searchButtonClicked)]) {
            [self.delegate searchButtonClicked];
        }
    }];
}

@end
